FROM rust:1.89.0 as builder

WORKDIR /app
# Install compile dependencies (protoc)
RUN apt-get update \
    && apt-get install -y --no-install-recommends protobuf-compiler \
    && rm -rf /var/lib/apt/lists/*

# Copy only the manifest file, warm up the dependency cache (to avoid downloading dependencies every time)
COPY Cargo.toml Cargo.lock ./
COPY pdata/Cargo.toml ./pdata/Cargo.toml
RUN mkdir -p src pdata/src \
    && echo 'fn main(){}' > src/main.rs \
    && echo 'pub fn _dummy(){}' > pdata/src/lib.rs \
    && cargo fetch \
    && cargo build --release || true

# Copy the complete source code and build
COPY . .
RUN cargo build --release

FROM debian:stable-slim
WORKDIR /app
COPY --from=builder /app/target/release/collector /app/collector
COPY collector-config.yml /app/collector-config.yml

EXPOSE 4317 4318

CMD ["/app/collector", "--config=/app/collector-config.yml"]